﻿using System.Windows;
using System.Windows.Threading;

namespace Brilliants.UI
{
    /// <summary>
    /// NbMessageBox.xaml 的交互逻辑
    /// </summary>
    public partial class NbMessageBox : NbWindow
    {

        private static int m_waitTime;

        public static NbMessageBox Notity(string message)
        {
            return Show(message, "提示", NbMessageBoxButtons.Ok, NbMessageBoxButtons.Ok);
        }
        public static NbMessageBox Notity(string message, int waitTime = 0, string OkText = "PASS", string CancelText = "NG")
        {
            m_waitTime = waitTime;
            return Show(message, "提示", NbMessageBoxButtons.Ok, NbMessageBoxButtons.Ok, OkText, CancelText);
        }
        public static NbMessageBox Show(string message)
        {
            return Show(message, "提示", NbMessageBoxButtons.Ok | NbMessageBoxButtons.Cancel, NbMessageBoxButtons.Ok);
        }
        public static NbMessageBox Show(string message, string title, NbMessageBoxButtons buttons, NbMessageBoxButtons focusButton, string OkText = "PASS", string CancelText = "NG")
        {
            var box = new NbMessageBox();
            Application.Current.Dispatcher.Invoke(() =>
            {
                box.MessageLabel.Text = message;
                box.TitleLabel.Content = title;
                box.OkButton.Content = OkText;
                box.CancelButton.Content = CancelText;
                if (m_waitTime > 0)
                {
                    var m_Timer = new DispatcherTimer();
                    m_Timer.Interval = new System.TimeSpan(10000000);
                    int i = 0;
                    m_Timer.Tick += new System.EventHandler((v, m) =>
                    {
                        box.MessageLabel.Text = message + (m_waitTime - i).ToString();
                        i++;
                    });
                    m_Timer.Start();
                }

                if (Application.Current.MainWindow != null && Application.Current.MainWindow != box)
                {
                    box.Owner = Application.Current.MainWindow;
                    box.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                }
                else
                {
                    box.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                }
                box.CancelButton.Visibility = buttons.HasFlag(NbMessageBoxButtons.Cancel) ? Visibility.Visible : Visibility.Collapsed;
                box.OkButton.Visibility = buttons.HasFlag(NbMessageBoxButtons.Ok) ? Visibility.Visible : Visibility.Collapsed;
                if (focusButton == NbMessageBoxButtons.Ok)
                    box.OkButton.Focus();
                else if (focusButton == NbMessageBoxButtons.Cancel)
                    box.CancelButton.Focus();
                box.Show();
            });
            return box;
        }


        public NbMessageBox()
        {
            InitializeComponent();
        }

        public NbMessageBoxResult NbDialogResult { get; set; } = NbMessageBoxResult.Cancel;

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.NbDialogResult = NbMessageBoxResult.Cancel;
            this.Close();
        }

        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            this.NbDialogResult = NbMessageBoxResult.Ok;
            this.Close();
        }
    }

    public enum NbMessageBoxResult
    {
        Ok = 0,
        Cancel = 1,
    }

    public enum NbMessageBoxButtons
    {
        Ok = 0,
        Cancel = 1,
    }
}
